package com.pickgliss.ui.controls.alert
{
	import com.pickgliss.geom.InnerRectangle;
	import com.pickgliss.ui.ComponentFactory;
	import com.pickgliss.ui.core.Component;
	import com.pickgliss.utils.ClassUtils;
	import com.pickgliss.utils.DisplayUtils;
	import com.pickgliss.utils.ObjectUtils;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	public class SimpleAlert extends BaseAlerFrame
	{
		public static const P_frameInnerRect:String = "frameInnerRect";
		public static const P_frameMiniH:String = "frameMiniH";
		public static const P_frameMiniW:String = "frameMiniW";
		public static const P_textField:String = "textFieldStyle";

		public function SimpleAlert()
		{
			super();
		}

		protected var _frameMiniH:int = int.MIN_VALUE;
		protected var _frameMiniW:int = int.MIN_VALUE;
		protected var _textField:TextField;
		protected var _textFieldStyle:String;
		private var _frameInnerRect:InnerRectangle;
		private var _frameInnerRectString:String;
		
		override public function dispose():void
		{
			if(_textField)ObjectUtils.disposeObject(_textField);
			_textField = null;
			_frameInnerRect = null;
			super.dispose();
		}
		
		/**
		 * 
		 * @param 弹出框与显示的文本框之间的显示外框参数
		 * 具体看OuterRectangle
		 * 例如：20,40,20,50
		 * 那么TextField将出现在20,40这个位置。
		 * TextField的宽度将是 20+TextField的宽度+20
		 * TextField的高度将是 40+TextField的宽度+50
		 */		
		public function set frameInnerRectString(value:String):void
		{
			if(_frameInnerRectString == value)return ;
			_frameInnerRectString = value;
			_frameInnerRect = ClassUtils.CreatInstance(ClassUtils.INNERRECTANGLE,ComponentFactory.parasArgs(_frameInnerRectString));
			onPropertiesChanged(P_frameInnerRect);
		}
		public function set frameMiniH(value:int):void
		{
			if(_frameMiniH == value)return;
			_frameMiniH = value;
			onPropertiesChanged(P_frameMiniH);
		}
		public function set frameMiniW(value:int):void
		{
			if(_frameMiniW == value)return;
			_frameMiniW = value;
			onPropertiesChanged(P_frameMiniW);
		}
		/**
		 * 
		 * @param value 中间文本框的样式
		 * 通过ComponentFactory创建
		 */		
		public function set textStyle(value:String):void
		{
			if(_textFieldStyle == value)return;
			if(_textField)ObjectUtils.disposeObject(_textField);
			_textFieldStyle = value;
			_textField = ComponentFactory.Instance.creat(_textFieldStyle);
			onPropertiesChanged(P_textField);
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			if(_textField)addChild(_textField);
		}
		
		protected function layoutFrameRect():void
		{
			var rectangle:Rectangle = _frameInnerRect.getInnerRect(_textField.width,_textField.height);
			if(rectangle.width > _frameMiniW)
			{
				_textField.x =  _frameInnerRect.para1;
				_width = rectangle.width;
			}else
			{
				_textField.x =  _frameInnerRect.para1+(_frameMiniW-rectangle.width)/2;
				_width = _frameMiniW;
			}
			if(rectangle.height > _frameMiniH)
			{
				_textField.y =  _frameInnerRect.para3;
				_height = rectangle.height;
			}else
			{
				_textField.y =  _frameInnerRect.para3+(_frameMiniH-rectangle.height)/2;
				_height = _frameMiniH;
			}
		}
		
		override protected function onProppertiesUpdate():void
		{
			if(_changedPropeties[P_info])
			{
				updateMsg();
				if(_frameInnerRect)
				{
					layoutFrameRect();
					_changedPropeties[Component.P_width] = true;
					_changedPropeties[Component.P_height] = true;
				}
			}
			super.onProppertiesUpdate();
		}
		
		protected function updateMsg():void
		{
			_textField.autoSize = TextFieldAutoSize.LEFT
			if(_info.mutiline)
			{
				_textField.multiline = true;
				if(!info.enableHtml)_textField.wordWrap = true;
				if(_info.textShowWidth > 0)
				{
					_textField.width = _info.textShowWidth;
				}else
				{
					_textField.width = DisplayUtils.getTextFieldMaxLineWidth(String(_info.data),_textField.defaultTextFormat,info.enableHtml);
				}
			}
			
			if(_info.enableHtml)
			{
				_textField.htmlText = String(_info.data);
			}else
			{
				_textField.text = String(_info.data);
			}
		}
	}
}